<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="赛事ID" prop="matchId">
        <el-input
          v-model="queryParams.matchId"
          placeholder="请输入赛事ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="展示状态" prop="showStatus">
        <el-select v-model="queryParams.showStatus" placeholder="请选择展示状态" clearable>
          <el-option
            v-for="dict in dict.type.hd_match_show_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['front:matchShow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdateTop"
          v-hasPermi="['front:matchShow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['front:matchShow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['front:matchShow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="matchShowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="match.matchTitle" />
      <el-table-column label="图片" align="center" prop="match.matchImg" >
        <template slot-scope="scope">
            <image-preview :src="scope.row.match.matchImg" :width="120" :height="70"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="展示状态" align="center" prop="showStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hd_match_show_status" :value="scope.row.showStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="展示排序" align="center" prop="showSort" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">


          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['front:matchShow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['front:matchShow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改赛事展示对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" center append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left"

      >
        <el-form-item label="赛事名称" prop="matchTitle">
          <div style="display: flex">

          <el-input v-model="form.matchTitle" placeholder="赛事名称" :disabled="true"/>
          <el-button type="primary" style="margin-left:10px;" @click="chooseMatch">赛事选择</el-button>
          </div>
        </el-form-item>
        <el-form-item label="赛事主图" prop="matchImg">
          <image-preview :src="form.matchImg" :width="120" :height="70"/>
        </el-form-item>
        <el-form-item label="状态" prop="showStatus">
          <el-radio-group v-model="form.showStatus">
            <el-radio
              v-for="dict in dict.type.hd_match_show_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="展示排序" prop="showSort">
          <el-input-number v-model="form.showSort" controls-position="right"  :min="1" />
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" round>取 消</el-button>
        <el-button type="primary" @click="submitForm('form')" round>确 定</el-button>
      </div>
    </el-dialog>
<!--    可选赛事列表-->
    <el-dialog width="50%" title="可选赛事"  :visible.sync="shShow" center append-to-body :close-on-click-modal="false">
      <!--查询-->
      <el-form :model="queryMatchParams" ref="queryMatchForm" size="small" :inline="true" v-show="true" label-width="68px">
        <el-form-item label="赛事标题" prop="matchTitle">
          <el-input
            v-model="queryMatchParams.matchTitle"
            placeholder="请输入赛事标题"
            clearable
            @keyup.enter.native="handleMatchQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleMatchQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetMatchQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!--列表-->
      <el-table ref="singleTable" :data="matchList" @cell-click="handleCurrentChange" :highlight-current-row="true" >
        <el-table-column label="赛事ID" align="center" prop="matchId" />
        <el-table-column label="赛事标题" align="center" prop="matchTitle" />
        <el-table-column label="赛事主图" align="center" prop="matchImg" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.matchImg" :width="50" :height="50"/>
          </template>
        </el-table-column>
      </el-table>
      <!--列表分页-->
      <pagination
        v-show="matchTotal>0"
        :total="matchTotal"
        :page.sync="queryMatchParams.pageNum"
        :limit.sync="queryMatchParams.pageSize"
        @pagination="getMatchList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listMatchShow, getMatchShow, delMatchShow, addMatchShow, updateMatchShow } from "@/api/front/matchShow";
import { listMatch} from "@/api/match/matchInfo";
export default {
  name: "MatchShow",
  dicts: ['hd_match_show_status'],
  data() {

    return {
      /**赛事数据*/
      shShow:false,//赛事弹出层
      matchList:[],//赛事数据
      // 查询赛事参数
      queryMatchParams: {
        pageNum: 1,
        pageSize: 10,
        matchTitle: null,
        matchImg: null,
        startTime: null,
        endTime: null,
        params:{matchNotShow:'yes'},//如果已经存在，则弹框中不显示已存在的赛事
        matchStatus:2,//已经结束的赛事
      },
      // 总条数
      matchTotal: 0,


/**赛事展示数据*/

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      matchTitle:'',
      matchImg:'',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 赛事展示表格数据
      matchShowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        matchId: null,
        showStatus: null,
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {

        matchTitle: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],

      }
    }
  },
  created() {
    this.getList();

  },
  methods: {
    /*点击选择赛事按钮*/
    chooseMatch(){
      this.shShow=true
      this.getMatchList()

    },
    /** 查询赛事列表 */
   getMatchList() {
      listMatch(this.queryMatchParams).then(response => {
        this.matchList = response.rows;
        this.matchTotal = response.total;
      });
    },
    /** 搜索按钮操作 */
    handleMatchQuery() {
      this.queryMatchParams.pageNum = 1;
      this.getMatchList();
    },
    /** 重置按钮操作 */
    resetMatchQuery() {
      this.resetForm("queryMatchForm");
      this.handleMatchQuery();
    },
//选择哪一条
    handleCurrentChange(val){

      if(val){
        this.form.matchTitle=val.matchTitle
        this.form.matchId=val.matchId
        this.form.matchImg=val.matchImg
        this.shShow=false

      }



    },

    /** 查询赛事展示列表 */
    getList() {
      this.loading = true;
      listMatchShow(this.queryParams).then(response => {
        this.matchShowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        matchShowId: null,
        matchId: null,
        matchTitle:'',
        showStatus: '0',
        showSort: 1,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据

    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.matchShowId)
      this.single = selection.length!==1
      this.multiple = !selection.length

      this.matchTitle=selection[0]?.match.matchTitle
      this.matchImg=selection[0]?.match.matchImg
      console.log(this.matchTitle)
      console.log(this.matchImg)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加赛事展示";
    },
    /** 修改按钮操作 */
    handleUpdateTop(){
      this.reset();
      const matchShowId = this.ids
      getMatchShow(matchShowId).then(response => {
        this.form = response.data;
        this.form.matchTitle= this.matchTitle
        this.form.matchImg= this.matchImg
        this.open = true;
        this.title = "修改赛事展示";
      });
    },



    handleUpdate(row) {
      this.reset();
      const matchShowId = row.matchShowId
      getMatchShow(matchShowId).then(response => {
        this.form = response.data;
        this.form.matchTitle= row.match.matchTitle
        this.form.matchImg= row.match.matchImg
        this.open = true;
        this.title = "修改赛事展示";
      });
    },
    /** 提交按钮 */
    submitForm(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          if (this.form.matchShowId != null) {

            updateMatchShow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMatchShow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const matchShowIds = row.matchShowId || this.ids;
      this.$modal.confirm('是否确认删除赛事展示编号为"' + matchShowIds + '"的数据项？').then(function() {
        return delMatchShow(matchShowIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('front/matchShow/export', {
        ...this.queryParams
      }, `matchShow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">

::v-deep{.el-dialog{border-radius:11px;}}
</style>
